package com.itheima.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.dto.CheckGroupDTO;
import com.itheima.mapper.CheckGroupMapper;
import com.itheima.mapper.GroupItemMapper;
import com.itheima.mapper.SetmealCheckGroupMapper;
import com.itheima.service.CheckGroupService;
import com.itheima.vo.CheckGroupVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CheckGroupServiceImpl implements CheckGroupService {
    @Autowired
    private CheckGroupMapper checkGroupMapper;
    @Autowired
    private GroupItemMapper groupItemMapper;
    @Autowired
    private SetmealCheckGroupMapper setmealCheckGroupMapper;

    /**
     * 分页查询检查组
     * @param queryPageBean
     * @return
     */
    public PageResult PageQuery(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());

        String queryString = queryPageBean.getQueryString();
        String code = null;
        String name = null;
        String helpCode = null;

        if (queryString != null && !queryString.isEmpty() && queryString.startsWith("0")) {

            //如果以0开头就是检查组编码
            code = queryString;
            Page<CheckGroupVO> page = checkGroupMapper.PageQuery(code, name, helpCode);
            return new PageResult(page.getTotal(), page.getResult());

        }else if (queryString != null && !queryString.isEmpty() && queryString.charAt(0) >= 'A' && queryString.charAt(0) <= 'Z') {

            //如果以大写字母开头就是助记码
            helpCode = queryString;
            Page<CheckGroupVO> page = checkGroupMapper.PageQuery(code, name, helpCode);
            return new PageResult(page.getTotal(), page.getResult());

        }else if (queryString != null && !queryString.isEmpty()) {

            //不为空的其他情况就是检查组名称
            name = queryString;

            Page<CheckGroupVO> page = checkGroupMapper.PageQuery(code, name, helpCode);
            return new PageResult(page.getTotal(), page.getResult());
        }else {
            Page<CheckGroupVO> page = checkGroupMapper.PageQuery(code, name, helpCode);
            return new PageResult(page.getTotal(), page.getResult());
        }
    }

    /**
     * 新增检查组
     * @param checkGroupDTO
     */
    @Transactional
    public void addCheckGroup(CheckGroupDTO checkGroupDTO, List<Integer> checkItemIds) {
        //1.插入检查组数据库表
        checkGroupMapper.addCheckGroup(checkGroupDTO);

        //2.给检查组中添加检查项（向检查组-检查项中间表插入数据）

        for (Integer checkItemId : checkItemIds) {
            groupItemMapper.addItemToGroup(checkGroupDTO.getId(), checkItemId);
        }
    }

    /**
     * 删除检查组
     * @param id
     */
    @Transactional
    public void deleteCheckGroup(Integer id) {
        //1.删除检查组数据库表
        checkGroupMapper.deleteCheckGroup(id);
        //2.删除检查组-检查项中间表数据
        groupItemMapper.deleteItemFromGroup(id);
    }

    /**
     * 根据id查询检查组
     * @param id
     * @return
     */
    public CheckGroupVO getById(Integer id) {
        return checkGroupMapper.getById(id);
    }

    /**
     * 修改检查组
     * @param checkGroupDTO
     */
    @Transactional
    public void update(CheckGroupDTO checkGroupDTO,List<Integer> checkItemIds) {
        //1.修改检查组数据库表
        checkGroupMapper.update(checkGroupDTO);

        //2.删除检查组-检查项中间表数据
        groupItemMapper.deleteItemFromGroup(checkGroupDTO.getId());

        //3.向检查组中添加检查项（向检查组-检查项中间表插入数据）
        for (Integer checkItemId : checkItemIds) {
            groupItemMapper.addItemToGroup(checkGroupDTO.getId(), checkItemId);
        }
    }

    /**
     * 查询所有检查组
     * @return
     */
    public List<CheckGroupVO> findAll() {
        return checkGroupMapper.findAll();
    }

    /**
     * 根据套餐id查询检查组
     * @param setmealId
     * @return
     */
    public List<Integer> findCheckGroupIdsBySetmealId(Integer setmealId) {
        return setmealCheckGroupMapper.findCheckGroupIdsBySetmealId(setmealId);
    }
}
